<!--
 * @Description: 
 * @Author:  huangshuai
 * @Date: 2023-11-08 11:28:41
 * @LastEditTime: 2023-11-08 14:33:57
 * @LastEditors: huangshuai 
-->
<template>
  <el-dialog v-model="dialogVisible" title="发表帖子" width="40%">
    <el-form
      :model="form"
      :inline="true"
      class="customClass"
      :rules="rules"
      ref="formRef"
    >
      <el-form-item label="标题" prop="titleName" label-width="80px">
        <el-input
          v-model="form.titleName"
          placeholder="请输入标题"
          style="width: 180px"
        />
      </el-form-item>
      <el-form-item label="类型" prop="categoryName" label-width="80px">
        <el-select
          v-model="form.categoryName"
          placeholder="请选择类型"
          style="width: 180px"
        >
          <el-option label="宠物" value="0" />
          <el-option label="生活" value="1" />
          <el-option label="其他" value="2" />
          <el-option label="无" value="" />
        </el-select>
      </el-form-item>

      <el-form-item label="内容" prop="content" label-width="80px">
        <el-input
          v-model="form.content"
          placeholder="请输入内容"
          style="width: 400px"
          type="textarea"
        />
      </el-form-item>
      <el-form-item
        label="评论区是否开放："
        label-width="140px"
        prop="commentAreaStatus"
      >
        <el-radio-group v-model="form.commentAreaStatus">
          <el-radio label="0" size="large">关闭评论区</el-radio>
          <el-radio label="1" size="large">开放评论区</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="帖子是否公开：" label-width="130px" prop="publicStatus">
        <el-radio-group v-model="form.publicStatus">
          <el-radio label="0" size="large">私密</el-radio>
          <el-radio label="1" size="large">公开</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="图片：" label-width="80px" prop="pictureUrl">
        <FilesUpload
          v-model:uidList="uidList"
          v-model:files="files"
          :uploadType="3"
          ref="fileRef"
          label="上传图片"
        ></FilesUpload>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submitForm(formRef)">
          确定
        </el-button>
        <el-button @click="resetForm(formRef)">取消</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import FilesUpload from "@/view/components/files-upload.vue";
import { addPost } from "@/api/owner";
import type { FormInstance, FormRules } from "element-plus";
import { ElMessage } from "element-plus";
import { uploadFile } from "@/api/file";

interface RuleForm {
  content: string;
  titleName: number | string;
  commentAreaStatus: string | number;
  categoryName: string | number;
  publicStatus: string | number;
  pictureUrl: string;
}
const formRef = ref<FormInstance>();
const form = reactive<RuleForm>({
  content: "",
  titleName: "",
  commentAreaStatus: "1",
  categoryName: "",
  publicStatus: "1",
  pictureUrl: "",
});

const rules = reactive<FormRules<RuleForm>>({
  content: [{ required: true, message: "请输入帖子的内容", trigger: "blur" }],

  titleName: [
    {
      required: true,
      message: "请输入帖子的标题",
      trigger: "blur",
    },
  ],

  commentAreaStatus: [
    {
      required: true,
      message: "请选择评论区状态",
      trigger: "change",
    },
  ],
  categoryName: [
    {
      required: true,
      message: "请选择帖子类型",
      trigger: "change",
    },
  ],
  publicStatus: [
    {
      required: true,
      message: "请选择帖子是否公开",
      trigger: "change",
    },
  ],
});
const dialogVisible = ref(false);

const uidList = ref([]);
const files = ref([]);
const fileRef = ref();
const urlList = ref<string>("");
const uidNameList = ref<string[]>([]);
const emits = defineEmits(["getDataList"]);
const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;

  await formEl.validate(async (valid) => {
    if (valid) {
      urlList.value = "";
      uidList.value.forEach(
        (item: { pictureRealPath: string; uuidName: string }) => {
          urlList.value += item.pictureRealPath;
        }
      );
      uidNameList.value = [];
      uidList.value.forEach(
        (item: { pictureRealPath: string; uuidName: string }) => {
          uidNameList.value.push(item.uuidName);
        }
      );

      uidNameList.value.forEach(async (item: string, index: number) => {
        await uploadFile(files.value[index], item, 3).then(() => {});
      });
      form.pictureUrl = urlList.value;
      await addPost(form).then((res: any) => {
        dialogVisible.value = false;
        ElMessage({
          message: res.message,
          type: "success",
        });
        // location.reload();
        resetForm(formRef.value);
        files.value = [];
        uidList.value = [];
        uidNameList.value = [];
        urlList.value = "";
        fileRef.value.resetFile();
        emits("getDataList");
      });
    }
  });
};

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  dialogVisible.value = false;
  formEl.resetFields();
};
const showDialog = () => {
  dialogVisible.value = true;
};
defineExpose({
  showDialog,
});
</script>
<style scoped lang="less">
.dialog-footer {
  display: flex;
  width: 100%;
  justify-content: center;
  margin-top: -40px;
}

.customClass {
  .el-form-item {
    margin-bottom: 30px;
  }
  width: 80%;
}
</style>
